<?php
/**
 * @Author: anchen
 * @Date:   2017-09-01 15:43:05
 * @Last Modified by:   anchen
 * @Last Modified time: 2017-09-02 09:46:40
 */
include('simple_html_dom.php');
include('db.php');
date_default_timezone_set('Asia/Shanghai');
header("Content-type: text/html; charset=utf-8");

$url = 'http://kj.cjcp.com.cn/gaopin/';

$content = file_get_contents($url);
$start = strpos($content,'<table');
$end = strpos($content,'</table>');
$len = $end - $start;
$str = substr($content,$start,$len);

//引用simple_html_dom 类
$html = new simple_html_dom();
$html->load($str);

//匹配类容
foreach($html->find('#n_bjkuai3')as $article){
    $item['leixing']     = trim($article->find('.leixing', 0)->plaintext);
    $qihao = $article->find('.qihao', 0)->plaintext;
    $item['qihao']    = preg_replace("/([\x80-\xff]+)/","",$qihao);

    $item['time'] = $article->find('td[class=time] span', 0)->title.' '.$article->find('td[class=time]', 0)->plaintext;
    $aa = [];

    foreach ($article->find('input[class=q_orange]') as $e){
        $aa[] = $e->value;
    }
    $kjhm = implode(',',$aa);
    $item['kjhm'] = $kjhm;
    $articles[]= $item;
}
$articles=auto_charset($articles);
//qq($articles);


//插入数据库
foreach($articles as $k=>$v){
    $data = $db->where('qihao='.$v['qihao'])->select('data');
    if(!$data){
        $db->insert('data',$v);
        $return = '插入数据成功--'.date('Y-m-d H:i:s',time());
    }else{
        $return ='数据已存在 最后更新时间-----'.$data[0]['time'];
    }
    $return = auto_charset( $return."\t\t\n",'utf-8','gbk');
    echo $return;
}










// 自动转换字符集 支持数组转换
function auto_charset($fContents, $from='gbk', $to='utf-8') {
    $from = strtoupper($from) == 'UTF8' ? 'utf-8' : $from;
    $to = strtoupper($to) == 'UTF8' ? 'utf-8' : $to;
    if (strtoupper($from) === strtoupper($to) || empty($fContents) || (is_scalar($fContents) && !is_string($fContents))) {
        //如果编码相同或者非字符串标量则不转换
        return $fContents;
    }
    if (is_string($fContents)) {
        if (function_exists('mb_convert_encoding')) {
            return mb_convert_encoding($fContents, $to, $from);
        } elseif (function_exists('iconv')) {
            return iconv($from, $to, $fContents);
        } else {
            return $fContents;
        }
    } elseif (is_array($fContents)) {
        foreach ($fContents as $key => $val) {
            $_key = auto_charset($key, $from, $to);
            $fContents[$_key] = auto_charset($val, $from, $to);
            if ($key != $_key)
                unset($fContents[$key]);
        }
        return $fContents;
    }
    else {
        return $fContents;
    }
}



     //抓取页面内容
function Curl1($url){
        $ch2 = curl_init();
        $user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";//模拟windows用户正常访问
        curl_setopt($ch2, CURLOPT_URL, $url);
        curl_setopt($ch2, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch2, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:'.Rand_IP(), 'CLIENT-IP:'.Rand_IP()));
//追踪返回302状态码，继续抓取
        curl_setopt($ch2, CURLOPT_HEADER, false); 
        curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true); 
        curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true);

        curl_setopt($ch2, CURLOPT_NOBODY, false);
        curl_setopt($ch2, CURLOPT_REFERER, 'http://www.baidu.com/');//模拟来路
        curl_setopt($ch2, CURLOPT_USERAGENT, $user_agent);
    curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);    //SSL 报错时使用
    curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, false);    //SSL 报错时使用
        $temp = curl_exec($ch2);
        curl_close($ch2);
        return $temp;
}

//随机IP
function Rand_IP(){

    $ip1id= round(rand(100000, 2550000) / 10000); //第一种方法，直接生成
    $ip2id= round(rand(600000, 2550000) / 10000); //第一种方法，直接生成
    $ip3id= round(rand(600000, 2550000) / 10000);
    $ip4id= round(rand(600000, 2550000) / 10000);
    //下面是第二种方法，在以下数据中随机抽取
    $arr_1 = array("218","218","66","66","218","218","60","60","202","204","66","66","66","59","61","60","222","221","66","59","60","60","66","218","218","62","63","64","66","66","122","211");
    $randarr= mt_rand(0,count($arr_1)-1);
    //$ip1id = $arr_1[$randarr];
    return $ip1id.".".$ip2id.".".$ip3id.".".$ip4id;
}



function qq($a){

    echo "<pre style='color:#cc0000;display: block;padding: 9px;margin: 0 0 10px;font-size: 15px;line-height: 1.42857143;word-break: break-all;word-wrap: break-word;background-color: #f5f5f5;border: 1px solid #ccc;border-radius: 4px;'>";

    print_r($a);

    echo '</pre>';

}
